package com.itheima.service.impl;

import com.itheima.dao.FileMapper;
import com.itheima.dao.WebConfigMapper;
import com.itheima.pojo.File;
import com.itheima.pojo.WebConfig;
import com.itheima.service.WebConfigService;
import com.itheima.utils.Result;
import com.itheima.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class WebConfigServiceImpl implements WebConfigService {

    @Autowired
    private WebConfigMapper webConfigMapper;
    @Autowired
    private FileMapper fileMapper;


    //获取网站配置
    @Override
    public WebConfig getWebConfig() {
        WebConfig webConfig1 = webConfigMapper.selectAll().get(0);

            File file = fileMapper.selectByPrimaryKey(webConfig1.getLogo());
            ArrayList<String> list = new ArrayList<>();
            if (file != null) {
                list.add(file.getPicUrl());
            }
            webConfig1.setPhotoList(list);

        return webConfig1;
    }


    //修改网站配置
    @Override
    public Result editWebConfig(WebConfig webConfig) {
        WebConfig key = webConfigMapper.selectByPrimaryKey(webConfig.getUid());
        if (key==null){
            return new Result<>(StatusCode.FAIL,"没有数据，无法修改");
        }

        webConfig.setUpdateTime(new Date());
        webConfigMapper.updateByPrimaryKeySelective(webConfig);
        return new Result<>(StatusCode.SUCCESS,"编辑成功");
    }

}
